import {
  fetchAppleItem,
  deletePerson,
  addDtl,
  deleteDtl,
} from "../../api/list";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    personId: "",
    name: "",
    kuType:0,
    phoneNumber: "",
    address: "",
    pictures: [],
    pingJia: "",
    guiGe: "",
    qty: 0,
    rukus: [],
    imgList: [],
    showDetail: false,
    totalQty: 0,
    showActions: false,
    actions: [{ name: "修改" }, { name: "删除", color: "#ee0a24" }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      personId: options.personId,
    });
  },

  getPersonDetail() {
    wx.showLoading({
      title: "加载中...",
      mask: true,
    });

    let params = {
      personId: this.data.personId,
    };
    fetchAppleItem(params).then((res) => {
      if (res) {
        this.setData({
          name: res.name,
          phoneNumber: res.phoneNumber,
          address: res.address,
          pingJia: res.pingJia,
          rukus: res.rukus,
          kuType:res.kuType,
          pictures: res.pictures,
          totalQty: res.totalQty,
        });
      }
      setTimeout(() => {
        wx.hideLoading();
      }, 600);
    });
  },
  previewImage(e) {
    let current = e.target.dataset.src;
    let imgList = [];
    for (let i = 0; i < this.data.pictures.length; i++) {
      imgList.push(this.data.pictures[i].pictureUrl);
    }
    wx.previewImage({
      current: current, //当前点击的图片链接
      urls: imgList, //图片数组
    });
  },
  goEdit() {
    this.setData({
      showActions: true,
    });
  },
  saveDtl() {
    if (this.data.guiGe && this.data.qty) {
      let params = {
        personId: parseInt(this.data.personId),
        guiGe: this.data.guiGe,
        qty: parseInt(this.data.qty),
      };
      addDtl(null, params)
        .then((res) => {
          if (res.code === 200) {
            this.setData({
              showDetail: false,
              guiGe: "",
              qty: 0,
            });
            this.getPersonDetail();
          }
        })
        .catch((err) => {
          wx.showToast({
            title: "保存错误：" + err.toString(),
            icon: "error",
          });
          setTimeout(() => {
            wx.navigateTo({
              url: "/pages/login/login",
            });
          }, 600);
        });
    } else {
      wx.showToast({
        title: "请输入规格和数量",
        icon: "error",
        zIndex: 999,
        mask: true,
      });
      return;
    }
  },
  deleteDtl(e) {
    wx.showModal({
      title: "警告",
      content: "您确认要删除数据吗?",
      complete: (res) => {
        if (res.cancel) {
        }

        if (res.confirm) {
          let _params = {
            rukuId: e.currentTarget.dataset.id,
          };
          deleteDtl(null, _params)
            .then((res) => {
              if (res.code === 200) {
                this.getPersonDetail();
              }
            })
            .catch((err) => {
              wx.showToast({
                title: "删除数据异常" + err.toString(),
              });
            });
        }
      },
    });
  },
  setGuiGe(e) {
    this.setData({
      guiGe: e.detail,
    });
  },
  setQty(e) {
    this.setData({
      qty: e.detail,
    });
  },
  openDtlDialog() {
    this.setData({
      showDetail: true,
    });
  },
  onClosePop() {
    this.setData({
      showDetail: false,
    });
  },
  onCloseAction() {
    this.setData({
      showActions: false,
    });
  },
  onSelectAction(e) {
    if (e.detail.name === "删除") {
      wx.showModal({
        title: "警告",
        content: "您确认要删除数据吗?",
        complete: (res) => {
          if (res.cancel) {
          }

          if (res.confirm) {
            let _params = {
              personId: this.data.personId,
            };
            deletePerson(null, _params)
              .then((res) => {
                if (res.code === 200) {
                  wx.navigateBack();
                }
              })
              .catch((err) => {
                wx.showToast({
                  title: "删除数据异常" + err.toString(),
                });
              });
          }
        },
      });
    } else if (e.detail.name === "修改") {
      wx.navigateTo({
        url: "/pages/edit/edit?personId=" + this.data.personId,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getPersonDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
